﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace DataLayer {
    public class DAOFactory {
        public static DAOFactory getInstance() {
            // TODO:
            /*
             * 
             * load database configuration from file,
             * so the user can set the preferences easily,
             * so if both table name and columns are specified
             * there, just the type of the object (T parameter)
             * is required
             * 
             * */
            if (instance == null) {
                instance = new DAOFactory();

                // tables initialization
                tableNames.Add("Student", "STUDENTS");
                tableNames.Add("Materijal", "MATERIJALI");
                tableNames.Add("Oprema", "OPREMA");
            }
            return instance;
        }

        public void addTable(string type, string tableName) {
            tableNames[type] = tableName.ToUpper();
        }

        // TODO: implicit unboxing

        public Object getDAO<T>() {
            string entityName = typeof(T).Name;
            return new DAO<T>(tableNames[entityName], typeof(T).GetProperties(), connectionString);
        }

        public Object getDAO<T>(List<string> columns) {
            string entityName = typeof(T).Name;
            //if (!tableNames.ContainsKey(entityName))
                //addTable(entityName, entityName + "i");
            return new DAO<T>(tableNames[entityName], columns, connectionString);
        }

        private DAOFactory() { } // singleton

        private static DAOFactory instance;

        // database settings
        private static string host = "localhost";
        private static string database = "resources";
        private static string uid = "root";
        private static string password = "";
        private static string connectionString =
            "Database=" + database + ";Data Source=" + host + ";User Id=" + uid + ";Password=" + password + ";";

        private static Dictionary<string, string> tableNames = new Dictionary<string, string>();
    }
}
